<?php

namespace app\index\controller;

use app\common\model\News as NewsModel;
use app\common\model\Cate as CateModel;

class News extends Base
{
    /**
     * [index  资讯列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new NewsModel;
        $cmodel = new CateModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            $map['states'] = ['not in' ,'2,-1'];
            $map['comid'] = $user['comid'];
            $count = $model->getCount($map);
            $allpage = intval(ceil($count / $this->size));//总页面
            $lists = $model->getlist($map, $this->from, $this->size,'id,title,cover,comid,addtime,read_num,content,cateid,istop,recommend,from,jread_num,intro,subtitle,states');
            foreach($lists as &$v){
                $cate = $cmodel->findData($v['cateid'],'id','cname');
                $http = substr($v['cover'],0,4);
                if($http != 'http'){
                    $v['cover'] = config('domain_name').$v['cover'];
                }
                $v['addtime'] = date('Y-m-d H:i:s',$v['addtime']);
                $v['cname'] = $cate['cname'];
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看资讯列表');
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);
    }
    /**
     * [index  资讯添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new NewsModel;
        $user = $this->user;
        try{
            $param['addtime'] = time();
            $param['comid'] = $user['comid'];
            $id = $model->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '添加失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'添加资讯'.$id);
        return admin_json(1, '添加成功');
    }
    /**
     * [edit  资讯编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new NewsModel;
        $user = $this->user;
        try{
            $id = $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '编辑失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'编辑资讯'.$id);
        return admin_json(1, '编辑成功');
    }
    /**
     * [del  资讯删除]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new NewsModel;
        $user = $this->user;
        try{
            $model->editData(['id'=>$param['id'],'states'=>-1]);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, '删除失败'.$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'删除资讯'.$param['id']);
        return admin_json(1, '删除成功');
    }
}